package com.jiusit.onePurchase.interceptor;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.cookie.SetCookie;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jiusit.common.utils.StringUtil;
import com.jiusit.onePurchase.model.OnePurchaseUser;

/**
 * BlogInterceptor 此拦截器仅做为示例展示，在本 demo 中并不需要
 */
public class UserInterceptor implements Interceptor {
	private static final Logger log = Logger.getLogger(UserInterceptor.class);

	public void intercept(Invocation ai) {
		ApiConfig apiConfig = new ApiConfig();

		// 配置微信 API 相关常量
		apiConfig.setToken(PropKit.get("token"));
		apiConfig.setAppId(PropKit.get("appId"));
		apiConfig.setAppSecret(PropKit.get("appSecret"));
		/**
		 * 是否对消息进行加密，对应于微信平台的消息加解密方式： 1：true进行加密且必须配置 encodingAesKey
		 * 2：false采用明文模式，同时也支持混合模式
		 */
		apiConfig
				.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		apiConfig.setEncodingAesKey(PropKit.get("encodingAesKey",
				"setting it in config file"));
		// 获取微信用户信息
		ApiConfigKit.setThreadLocalApiConfig(apiConfig);
		Controller controller = ai.getController();
		ai.invoke();
		String agent=controller.getRequest().getHeader("User-Agent").toLowerCase();
		if(agent.indexOf("micromessenger")<=0){
			try {
				controller.getResponse().sendRedirect("http://api.fansiji.com/onePurchase_wb/home/homePage");
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}
		String access_token = "";
		String path = controller.getRequest().getRequestURI();
		log.debug("UserInterceptor  path:" + path);
		String from_user = controller.getCookie("from_user9");
		HttpServletRequest request=controller.getRequest();
		if(!StringUtil.isEmpty(request.getParameter("id"))){
			path+="?id="+request.getParameter("id");
		}
		System.out.println("UserInterceptor Cookie from_user 1:"+from_user);
		if (StringUtil.isEqual(from_user, "undefined") || from_user == null || from_user.equals("")) {
			from_user = controller.getAttrForStr("from_user9");
			System.out.println("UserInterceptor Attribute from_user:" + from_user);
			if (from_user == null || from_user.equals("")) {
				try {
					// 获取from_user
					String code = controller.getPara("code");
					if (code == null || "".equals(code)) {
						String redirect_uri1_0 = URLEncoder.encode(
								PropKit.get("host_uri",
										"setting it in config file") + path,
								"utf-8");
						String m1_0 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
								+ PropKit.get("appId")
								+ "&redirect_uri="
								+ redirect_uri1_0
								+ "&response_type=code&scope=snsapi_userinfo&state=defaultToHome#wechat_redirect";
						controller.redirect(m1_0);
						return;
					}
										String userAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
							+ PropKit.get("appId")
							+ "&secret="
							+ PropKit.get("appSecret")
							+ "&code="
							+ code
							+ "&grant_type=authorization_code";
					String userAccessTokenData = HttpKit
							.get(userAccessTokenUrl);
					log.debug("userAccessTokenData:" + userAccessTokenData);
					// 注意userAccessToken一天1000次与2小时限制
					ObjectMapper mapper = new ObjectMapper();
					Map data = mapper.readValue(userAccessTokenData, Map.class);
					if (data.get("openid") != null) {
						String openId = data.get("openid").toString();
						System.out.println("openId:" + openId);
						access_token = data.get("access_token").toString();
						String dataStr=HttpKit.get("https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openId+"&lang=zh_CN");
						Map <String,Object> map=StringUtil.getMap(dataStr);
						String nickname = (String) map.get("nickname");
						nickname=URLEncoder.encode(nickname);
						System.out.println(nickname);
						Integer sex = (Integer) map.get("sex");
						String headimgurl = (String) map.get("headimgurl");
						if(!StringUtil.isEmpty(headimgurl)){
						StringUtil.replace(headimgurl, "\\/", "/");
						}
						OnePurchaseUser user = OnePurchaseUser.dao.findFirst(
								"select * from purchase_users where wx_id=?",
								openId);
						if (user == null) {
							String user_id = StringUtil.getUUID();
							OnePurchaseUser purchaseUser = new OnePurchaseUser()
									.set("id", user_id)
									.set("nick_name", nickname)
									.set("real_name", nickname).set("sex", sex)
									.set("wx_id", openId)
									.set("head_path", headimgurl)
									.set("is_follow", "0")
									.set("amount_money", 0.00)
									.set("login_num", 1)
									.set("create_by", user_id)
									.set("create_name", nickname)
									.set("create_date", new Date())
									.set("update_by", user_id)
									.set("update_name", nickname)
									.set("update_date", new Date());
							purchaseUser.save();

						}else{
							int login_num=user.getInt("login_num")+1;
							OnePurchaseUser purchaseUser = new OnePurchaseUser()
							.set("id", user.get("id"))
							.set("nick_name", nickname)
							.set("real_name", login_num)
							.set("sex", sex)
							.set("login_num", login_num)
							.set("head_path", headimgurl)
							.set("update_by", user.get("id"))
							.set("update_name", nickname)
							.set("update_date", new Date());
					        purchaseUser.update();
						}
						from_user = openId;
						System.out.println(from_user);
						Cookie cookie = new Cookie("from_user9", from_user);
						cookie.setMaxAge(24 * 60 * 60 * 30);
						cookie.setPath("/");
						controller.setCookie(cookie);
						System.out.println("UserInterceptor Cookie from_user 2:"+controller.getCookie("from_user9"));
					} else {
						log.debug("get openId fail ");
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			  }
			}else{
				Cookie cookie = new Cookie("from_user9", from_user);
				cookie.setMaxAge(24 * 60 * 60 * 30);
				cookie.setPath("/");
				controller.setCookie(cookie);
				System.out.println("UserInterceptor Cookie from_user 3:"+controller.getCookie("from_user9"));
				Db.update("update purchase_users set login_num=login_num+1 where wx_id='"+from_user+"'");
			}
	}
	
}
